Hướng dẫn nhúng Sentry vào dự án
I. Điều kiện tiên quyết
- Đã cài đặt Nodejs, gói Sentry.
- Có quyền truy cập git.
- Có quyền truy cập server ssh gcalls.
II. Đối tượng
- Customer success engineer
III. Nội dung chi tiết
Bước 1: Vào index.js có chứa thông tin render của dự án. Insert đoạn code sau vào.
Đối với React App :
Install package sentry vào React App.
npm i @sentry/tracing @sentry/reactVào Index.js insert code trước ReactDom.render của React và import thư viện sentry vào.
Sentry.init({
dsn: process.env.REACT_APP_SENTRY_DSN,
environment: process.env.REACT_APP_ENV,
integrations: [new Integrations.BrowserTracing()],
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 1,
})Có thể khởi tạo enviroment cho từng ứng dụng để phân biệt. Ví dụ : enviroment: process.env.REACT_APP_ENV_CALLBOX_DEMO (REACT_APP_ENV_CALLBOX_DEMO=CALLBOX_DEMO)
Chú thích :
- dsn: dsn để truy cập sentry.
- enviroment: Mội truờng truy cập.
- tracesSampleRate: Tỉ lệ rate để bắt lỗi.
Tài liệu tham khảo thêm : Tài liệu tham khảo sentry với react trích dẫn từ Tài liệu trích dẫn
Đối với expressJS:
Install package sentry vào dự án:
npm install --save @sentry/node @sentry/tracingVào file index.js và install đoạn code sau vào:
const express = require('express');
const Sentry = require('@sentry/node');
const Tracing = require("@sentry/tracing");
Sentry.init({
dsn: "process.env.NODE_EXPRESS_SENTRY_ENV",
integrations: [
new Sentry.Integrations.Http({ tracing: true }),
new Tracing.Integrations.Express({ app }),
],
tracesSampleRate: 1.0,
});
app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());
app.use(Sentry.Handlers.errorHandler());
app.use(function onError(err, req, res, next) {
res.statusCode = 500;
res.end(res.sentry + "\n");
});Có thể khởi tạo enviroment cho từng ứng dụng để phân biệt. Ví dụ : enviroment: process.env.REACT_APP_ENV_SERVICE_DEMO (REACT_APP_ENV_SERVICE_DEMO=SERVICE_DEMO)
Chú thích :
- dsn: dsn để truy cập sentry.
- enviroment: Mội truờng truy cập.
- tracesSampleRate: Tỉ lệ rate để bắt lỗi.
Ở mặc định, errorHandler chỉ bắt những lỗi từ 500 đổ lện. Dưới đây là ví dụ cách custom lại để bắt lỗi theo ý muốn :
app.use(
Sentry.Handlers.errorHandler({
shouldHandleError(error) {
// Capture all 404 and 500 errors
if (error.status === 404 || error.status === 500) {
return true;
}
return false;
},
})
);Tài liệu tham khảo : Tài liệu tham khảo sentry with express trích dẫn từ Tài liệu trích dẫn
Đối với env xin vui lòng truy cập server Demo hoặc production của callbox để biết thêm chi tiết. Đối với dsn người dùng có thể truy cập vào sentry của gcalls để lấy :

IV. Kết luận
Tài liệu tham khảo được trích dẫn từ Tài liệu trích dẫn, ngày 11-03-2023 và có thể được thay đổi theo thời gian .
Bài viết hướng dẫn người dùng cách nhúng Sentry vào dự án. Nếu có thắc mắc vui lòng tạo issues hoặc liên hệ.